package web.beans;

import entity.User.bo.UserBo;
import entity.User.model.User;
import util.Mail;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import java.io.Serializable;
import java.util.Random;


/**
 * Created by vanches on 3.1.15.
 */
public class RegisterBean implements Serializable{

    private String login;
    private String pass;
    private String email;
    private String checkCode;
    private boolean codeSent=false;
    private UserBo userBo;
    private Mail mail;


    //region The secret code
    public String sendCode (){
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Код отправлен",  "Your message: ") );
        checkCode=generateCode();

        try{
        mail.sendMail(email, email, email,"Ваш проверочный код : "+checkCode);
        codeSent=true;}
        catch (Exception e){
            throw new ValidatorException(new FacesMessage("Нет соединение с сервером"));
        }

        return null;
    }
    private String generateCode(){
        Random rnd =new Random();
        return ""+100 + rnd.nextInt(100000 - 100 + 1);
    }
    public void verificationСode(FacesContext context,
                                 UIComponent toValidate,
                                 Object value) throws ValidatorException {
        String codeUs = (String) value;
        if (!checkCode.equals(codeUs)) {
            FacesMessage message = new FacesMessage("Проверочное число введено неверно!");
            throw new ValidatorException(message);
        }
    }

    //endregion
    //region Register
    public String addUser(){
        userBo.save(new User(login,pass,email));
        return "quest";
    }
    public void checkLogin(FacesContext context,
                           UIComponent toValidate,
                           Object value) throws ValidatorException {
        String login = (String) value;

        if (userBo.checkLogin(login)) {
            FacesMessage message = new FacesMessage("Логин занят!" );
            throw new ValidatorException(message);
        }
    }
    //endregion
    //region GET and SET

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCheckCode() {
        return checkCode;
    }

    public void setCheckCode(String checkCode) {
        this.checkCode = checkCode;
    }

    public boolean isCodeSent() {
        return codeSent;
    }

    public void setCodeSent(boolean codeSent) {
        this.codeSent = codeSent;
    }

    public UserBo getUserBo() {
        return userBo;
    }

    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    public Mail getMail() {
        return mail;
    }

    public void setMail(Mail mail) {
        this.mail = mail;
    }
    //endregion
}
